package org.example.postgraduateproject.Dao.imp;

import org.example.postgraduateproject.Dao.AnswerDao;
import org.example.postgraduateproject.Utils.DbUtils;
import org.example.postgraduateproject.Utils.jdbcUtils;
import org.example.postgraduateproject.entity.Answer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wu
 * @date 2025-05-28
 */
public class AnswerDaoImp implements AnswerDao {

    @Override
    public List<Answer> selectAllAnswers() {
        StringBuilder sql = new StringBuilder("SELECT answer_id, question_id, user_id, content, created_at, updated_at, is_accepted, votes FROM answers");
        List<Answer> answers = new ArrayList<>();
        Connection conn = DbUtils.getConnection();
        ResultSet rst = null;

        try {
            rst = jdbcUtils.executeQuery(conn, sql);
            while (rst.next()) {
                Answer answer = new Answer();
                answer.setAnswerId(rst.getInt("answer_id"));
                answer.setQuestionId(rst.getInt("question_id"));
                answer.setUserId(rst.getInt("user_id"));
                answer.setContent(rst.getString("content"));
                answer.setCreatedAt(rst.getTimestamp("created_at"));
                answer.setUpdatedAt(rst.getTimestamp("updated_at"));
                answer.setIsAccepted(rst.getBoolean("is_accepted"));
                answer.setVotes(rst.getInt("votes"));
                answers.add(answer);
            }
            return answers;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(rst, conn, null);
        }
    }

    @Override
    public Answer selectAnswerById(int answerId) {
        StringBuilder sql = new StringBuilder("SELECT answer_id, question_id, user_id, content, created_at, updated_at, is_accepted, votes FROM answers WHERE answer_id = ?");
        Connection conn = DbUtils.getConnection();
        ResultSet rst = null;

        try {
            rst = jdbcUtils.executeQuery(conn, sql, answerId);
            if (rst.next()) {
                Answer answer = new Answer();
                answer.setAnswerId(rst.getInt("answer_id"));
                answer.setQuestionId(rst.getInt("question_id"));
                answer.setUserId(rst.getInt("user_id"));
                answer.setContent(rst.getString("content"));
                answer.setCreatedAt(rst.getTimestamp("created_at"));
                answer.setUpdatedAt(rst.getTimestamp("updated_at"));
                answer.setIsAccepted(rst.getBoolean("is_accepted"));
                answer.setVotes(rst.getInt("votes"));
                return answer;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(rst, conn, null);
        }
        return null;
    }

    @Override
    public List<Answer> selectAnswersByQuestionId(int questionId) {
        StringBuilder sql = new StringBuilder("SELECT answer_id, question_id, user_id, content, created_at, updated_at, is_accepted, votes FROM answers WHERE question_id = ? ORDER BY is_accepted DESC, votes DESC");
        List<Answer> answers = new ArrayList<>();
        Connection conn = DbUtils.getConnection();
        ResultSet rst = null;

        try {
            rst = jdbcUtils.executeQuery(conn, sql, questionId);
            while (rst.next()) {
                Answer answer = new Answer();
                answer.setAnswerId(rst.getInt("answer_id"));
                answer.setQuestionId(rst.getInt("question_id"));
                answer.setUserId(rst.getInt("user_id"));
                answer.setContent(rst.getString("content"));
                answer.setCreatedAt(rst.getTimestamp("created_at"));
                answer.setUpdatedAt(rst.getTimestamp("updated_at"));
                answer.setIsAccepted(rst.getBoolean("is_accepted"));
                answer.setVotes(rst.getInt("votes"));
                answers.add(answer);
            }
            return answers;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(rst, conn, null);
        }
    }

    @Override
    public List<Answer> selectAnswersByUserId(int userId) {
        StringBuilder sql = new StringBuilder("SELECT answer_id, question_id, user_id, content, created_at, updated_at, is_accepted, votes FROM answers WHERE user_id = ? ORDER BY created_at DESC");
        List<Answer> answers = new ArrayList<>();
        Connection conn = DbUtils.getConnection();
        ResultSet rst = null;

        try {
            rst = jdbcUtils.executeQuery(conn, sql, userId);
            while (rst.next()) {
                Answer answer = new Answer();
                answer.setAnswerId(rst.getInt("answer_id"));
                answer.setQuestionId(rst.getInt("question_id"));
                answer.setUserId(rst.getInt("user_id"));
                answer.setContent(rst.getString("content"));
                answer.setCreatedAt(rst.getTimestamp("created_at"));
                answer.setUpdatedAt(rst.getTimestamp("updated_at"));
                answer.setIsAccepted(rst.getBoolean("is_accepted"));
                answer.setVotes(rst.getInt("votes"));
                answers.add(answer);
            }
            return answers;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(rst, conn, null);
        }
    }

    @Override
    public int insertAnswer(Answer answer) {
        StringBuilder sql = new StringBuilder("INSERT INTO answers (question_id, user_id, content, is_accepted, votes) VALUES (?, ?, ?, ?, ?)");
        Connection conn = DbUtils.getConnection();

        try {
            return jdbcUtils.executeUpdate(conn, sql,
                    answer.getQuestionId(),
                    answer.getUserId(),
                    answer.getContent(),
                    answer.getIsAccepted(),
                    answer.getVotes()
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(null, conn, null);
        }
    }

    @Override
    public int updateAnswer(Answer answer) {
        StringBuilder sql = new StringBuilder("UPDATE answers SET question_id = ?, user_id = ?, content = ?, is_accepted = ?, votes = ?, updated_at = NOW() WHERE answer_id = ?");
        Connection conn = DbUtils.getConnection();

        try {
            return jdbcUtils.executeUpdate(conn, sql,
                    answer.getQuestionId(),
                    answer.getUserId(),
                    answer.getContent(),
                    answer.getIsAccepted(),
                    answer.getVotes(),
                    answer.getAnswerId()
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(null, conn, null);
        }
    }

    @Override
    public int deleteAnswer(int answerId) {
        StringBuilder sql = new StringBuilder("DELETE FROM answers WHERE answer_id = ?");
        Connection conn = DbUtils.getConnection();

        try {
            return jdbcUtils.executeUpdate(conn, sql, answerId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.Close(null, conn, null);
        }
    }
}